package com.irdstudio.efp.esb.api.rest.zzd;

import java.math.BigDecimal;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.efp.esb.api.bean.zzd.ReqLoanResQryBean;
import com.irdstudio.efp.esb.api.bean.zzd.ResLoanResuQryBean;
import com.irdstudio.efp.esb.api.rest.EsbServerController;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.InterfaceCheckUtils;
import com.irdstudio.efp.nls.service.facade.NlsApplyInfoService;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;

/**
 * 用户放款结果查询接口controller
 * @author qiuyf
 * @since 2018年12月18日 下午7:42:06
 * @version 1.0
 */
public class LoanResultQueryController implements EsbServerController{

	/**
	 * 日志记录变量
	 */
	private static Logger logger = LoggerFactory.getLogger(LoanResultQueryController.class);
	
	@Autowired
	@Qualifier("nlsApplyInfoService")
	private NlsApplyInfoService nlsApplyInfoService;
	
	@Override
	public String execute(String esbReqBodyBean) throws ESBException, Exception {
		// 定义请求对象
		ReqLoanResQryBean reqLoanResQryBean = null;
		// 定义返回对象
		String esbRespBodyBean = null;
		// 定义返回报文对象
		ResLoanResuQryBean returnBean = new ResLoanResuQryBean();
		// 订单申请信息
		NlsApplyInfoVO naInfo = new NlsApplyInfoVO();
		// 订单号
		String applSeq = "";
		try {
			logger.info(LoanResultQueryController.class.getName() + "用户放款结果查询接口请求报文:" + esbReqBodyBean);
			// 1.将请求报文对象转成相应的json
			reqLoanResQryBean = JSONObject.parseObject(esbReqBodyBean, ReqLoanResQryBean.class);
			applSeq = reqLoanResQryBean.getApplSeq();
			
			logger.info(LoanResultQueryController.class.getName() + "根据理财通订单号：" + applSeq +  "调用用户放款结果查询接口，处理开始-----------------------");
			
			// 2.必输项校验
			this.checkRequired(reqLoanResQryBean);
			
			// 3.封装查询条件
			//贷款订单号
			naInfo.setApplySeq(reqLoanResQryBean.getApplSeq());  //申请流水号 = 贷款订单号
			
			// 4.调用对应中台服务处理
			NlsApplyInfoVO result = nlsApplyInfoService.queryByPk(naInfo);

			// 5.拼装返回报文,判断查询结果： 空，查询失败；非空，查询成功
			if(Objects.nonNull(result)) {
				// 返回码
				returnBean.setRetCode(EsbBizEnums.RetCodeEnum.SUCCESS.VALUE);
				// 订单号
				returnBean.setApplSeq(result.getApplySeq().toString());
				// 征信查询时间 TODO
				returnBean.setPbocCreditTime(naInfo.getPbocAuthLetter());
				// 转账备注
				returnBean.setAttach(result.getRemarks());
			}else {
				returnBean.setRetCode(EsbBizEnums.RetCodeEnum .FAILURE.VALUE);
				returnBean.setRetMsg("暂无放款记录！");
				//银行内部错误码
				//银行内部错误原因
			}
			// 6.object转成JSON string
			esbRespBodyBean = JSON.toJSONString(returnBean);
			
			logger.info("调用用户放款结果查询接口，处理结束。。。。。。。。。。。。。。。");
		} catch (ESBException e) {
			logger.error("调用用户放款结果查询接口出错，错误信息为："+e.getMessage());
			throw new ESBException();
		} catch (Exception e) {
			logger.error("调用用户放款结果查询接口出错，错误信息为："+e.getMessage());
		}
		
		return esbRespBodyBean;
	}
	
	/**
	  * 用户放款结果查询请求报文必输项校验
	  * @param reqLoanResQryBean  用户放款结果查询请求报文bean
	  * @throws ESBException
	  * @author qiuyf
	  * @since 2018年12月19日 上午10:40:38
	  * @version 1.0
	 */
	private void checkRequired(ReqLoanResQryBean reqLoanResQryBean) throws ESBException {
		//协议参数
		InterfaceCheckUtils.getValue("signType", "签名方式", reqLoanResQryBean.getSignType(), true);
		InterfaceCheckUtils.getValue("serviceVersion", "接口版本", reqLoanResQryBean.getServiceVersion(), true);
		InterfaceCheckUtils.getValue("inputCharset", "字符集", reqLoanResQryBean.getInputCharset(), true);
		InterfaceCheckUtils.getValue("sign", "签名", reqLoanResQryBean.getSign(), true);
		InterfaceCheckUtils.getValue("signKeyIndex", "密钥序号", reqLoanResQryBean.getSignKeyIndex(), true);
		//业务参数
		InterfaceCheckUtils.getValue("userId", "真身id", reqLoanResQryBean.getUserId(), true);
		InterfaceCheckUtils.getValue("bankCusId", "银行客户号", reqLoanResQryBean.getBankCusId(), true);
		InterfaceCheckUtils.getValue("applSeq", "贷款订单号", reqLoanResQryBean.getApplSeq(), true);
		InterfaceCheckUtils.getValue("loanAmt", "贷款金额", reqLoanResQryBean.getLoanAmt(), true);
		InterfaceCheckUtils.getValue("curType", "币种", reqLoanResQryBean.getCurType(), true);
		
	}

}